/*
 * @lc app=leetcode.cn id=946 lang=javascript
 *
 * [946] 验证栈序列
 */

// @lc code=start
/**
 * @param {number[]} pushed
 * @param {number[]} popped
 * @return {boolean}
 */
var validateStackSequences = function(pushed, popped) {
   const stack = [];
   let j = 0;
   for(let i = 0; i < pushed.length; i++) {
       stack.push(pushed[i]);
       while(stack.length && stack[stack.length - 1] === popped[j]) {
           stack.pop();
           j++;
       }
   }
   return !stack.length;
};
// @lc code=end

